//
//  QpAllCityViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/10.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpAllCityViewController.h"

@interface QpAllCityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataArr;


@end

@implementation QpAllCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.navigationItem.title = @"全部城市";
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    

    //数据处理
    [self dataHandler];
    
    /**<创建collectionView*/
    [self creatCollectionView];
    
}
- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)dataHandler
{
    // 通过传过来的热门国家id拼接网址
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@",self.countryid];
    NSString *str = @"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"%@%@",url,str] completion:^(id result) {
       
        self.dataArr = [QpAllCityModel modelHanderWithArray:result[@"data"]];
        [_collectionView reloadData];
        
        
    }];
    
}

- (void)creatCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置滚动方向(竖向)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置cell大小
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 40) / 2, ((SCREEN_WIDTH - 30 - 10) / 2) * 226 / 339 + 60);
    //设置行列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    //初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - 74) collectionViewLayout:layout];
    //设置代理人
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    //隐藏滚动条
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    
    _collectionView.backgroundColor = [UIColor whiteColor];
//    [_collectionView NightWithType:UIViewColorTypeNormal];
    //添加父视图
    [self.view addSubview:_collectionView];
    
    //注册重用池
    
    [_collectionView registerClass:[QpAllCityCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

#pragma mark - 两个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QpAllCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    QpAllCityModel *qpAll = _dataArr[indexPath.item];
    //  获取网络图片
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:qpAll.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    // 中文名字拼接英文名字
    cell.catename.text = [NSString stringWithFormat:@"%@ %@",qpAll.catename,qpAll.catename_en];
    [cell.catename NightWithType:UIViewColorTypeNormal];
    // 代表景点
    cell.representative.text = [NSString stringWithFormat:@"代表景点:%@",qpAll.representative];
    [cell.representative NightWithType:UIViewColorTypeNormal];
    cell.backgroundColor = [UIColor whiteColor];
 [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}
#pragma mark - 点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 跳转隐藏 tabbar
    self.hidesBottomBarWhenPushed=YES;
    QpCityDetailsViewController *qp = [[QpCityDetailsViewController alloc]init];
    QpAllCityModel *q = _dataArr[indexPath.item];
    qp.cityid = q.cityId;
    qp.str = q.catename;
    [self.navigationController pushViewController:qp animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
